﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xcj.Mes.WholeLine.DataBase.Entity;
using Xcj.Mes.WholeLine.DataBase.Service;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.Common
{
    public class TestDataGenerator
    {
        private static readonly Random random = new Random();
        private static readonly string[] deviceTypes = new[] { "钉卷", "组立", "老化", "座板" };
        private static readonly string[] notGoodNames = new[]
        {
        "短路不良", "启动不良", "胶带不良", "正黑胶带不良", "负黑胶带不良",
        "正极CCD不良", "负极CCD不良", "外观检测不良", "反极检测不良", "自动排料"
    };

        public static void BatchAndNotGoodData()
        {
            var batchEntities = new List<BatchEntity>();
            var notGoodEntities = new List<NotGoodEntity>();

            // 为每个设备类型生成3个批次数据
            foreach (var deviceType in deviceTypes)
            {
                for (int i = 1; i <= 3; i++)
                {
                    var batch = CreateBatchEntity(deviceType, i);
                    batchEntities.Add(batch);

                    // 为每个批次生成不良数据
                    var notGoods = CreateNotGoodEntities(batch);
                    notGoodEntities.AddRange(notGoods);
                }
            }

            // 插入批次数据
            foreach (var batch in batchEntities)
            {
                using (BatchService service = new BatchService())
                {
                    service.Add(batch);
                }
            }

            // 插入不良数据
            foreach (var notGood in notGoodEntities)
            {
                using (NotGoodService service = new NotGoodService())
                {
                    service.Add(notGood);
                }
            }
        }

        private static BatchEntity CreateBatchEntity(string deviceType, int index)
        {
            int outputCount = random.Next(1000, 10000);
            int goodCount = random.Next(800, outputCount - 100);
            int notGoodCount = outputCount - goodCount;
            double yield = Math.Round((double)goodCount / outputCount * 100, 2);

            return new BatchEntity()
            {
                DeviceSn = "DS-01",
                DeviceType = deviceType,
                BatchNumber = $"BN-{deviceType}-{index:00}",
                BatchName = $"Batch-{deviceType}-{index:00}",
                BatchSerial = $"SER-{random.Next(1000, 9999)}",
                BatchSpecification = $"SPEC-{random.Next(1, 5)}",
                BatchSize = $"SIZE-{random.Next(1, 10)}",
                Schedule = random.Next(0, 2) == 0 ? "白班" : "夜班",
                OutputCount = outputCount,
                GoodCount = goodCount,
                NotGoodCount = notGoodCount,
                Yield = yield
            };
        }

        private static List<NotGoodEntity> CreateNotGoodEntities(BatchEntity batch)
        {
            var notGoods = new List<NotGoodEntity>();
            int remainingNotGoodCount = batch.NotGoodCount;

            // 随机选择3-6种不良类型
            int notGoodTypeCount = random.Next(3, 7);
            var selectedNotGoodNames = GetRandomNotGoodNames(notGoodTypeCount);

            // 为前n-1种不良类型分配随机数量
            for (int i = 0; i < selectedNotGoodNames.Count - 1; i++)
            {
                if (remainingNotGoodCount <= 0) break;

                int maxCount = Math.Min(remainingNotGoodCount, random.Next(1, remainingNotGoodCount / 2 + 1));
                int count = random.Next(1, maxCount + 1);

                notGoods.Add(new NotGoodEntity()
                {
                    BatchID = batch.ID, // 假设BatchEntity有ID属性
                    Name = selectedNotGoodNames[i],
                    Count = count
                });

                remainingNotGoodCount -= count;
            }

            // 最后一种不良类型分配剩余数量
            if (remainingNotGoodCount > 0 && selectedNotGoodNames.Count > 0)
            {
                notGoods.Add(new NotGoodEntity()
                {
                    BatchID = batch.ID,
                    Name = selectedNotGoodNames[selectedNotGoodNames.Count - 1],
                    Count = remainingNotGoodCount
                });
            }

            return notGoods;
        }

        private static List<string> GetRandomNotGoodNames(int count)
        {
            var shuffled = new List<string>(notGoodNames);

            // Fisher-Yates洗牌算法
            for (int i = shuffled.Count - 1; i > 0; i--)
            {
                int j = random.Next(0, i + 1);
                var temp = shuffled[i];
                shuffled[i] = shuffled[j];
                shuffled[j] = temp;
            }

            return shuffled.GetRange(0, Math.Min(count, shuffled.Count));
        }
    }
}
